/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.leetCode.other.array.easy;

/**
 *
 *@author : sunzb
 *@date: 2019/5/17
 */
public class NonDecreasingArray_665 {

    public boolean checkPossibility(int[] nums) {
//        array[i] <= array[i + 1]
        int c = 1;
        if(nums.length < 3) return true;
        if(nums[0] > nums[1]) c--;
        for(int i = 2; i < nums.length; i++){
            if(nums[i-1] > nums[i]){
                c--;
                if(c < 0) return false;
                if(nums[i - 2] > nums[i]) nums[i] = nums[i-1];
            }
        }
        return true;
    }

    public static void main(String[] args) {
        /*// true
        System.out.println(new NonDecreasingArray_665().checkPossibility(
                new int[] {4,2,3}
        ));
        // false
        System.out.println(new NonDecreasingArray_665().checkPossibility(
                new int[] {4,2,1}
        ));
        // false
        System.out.println(new NonDecreasingArray_665().checkPossibility(
                new int[] {4, 4, 2,3}
        ));*/
        // false
        System.out.println(new NonDecreasingArray_665().checkPossibility(
                new int[] {3,4,2,3}
        ));
        // true
        System.out.println(new NonDecreasingArray_665().checkPossibility(
                new int[] {2,3,3,2,4}
        ));

        System.out.println(new NonDecreasingArray_665().checkPossibility(
                new int[] {2,6,3,4}
        ));
    }
}
